package com.ssm.modules.controller;


import com.ssm.modules.entity.Log;
import com.ssm.modules.entity.User;
import com.ssm.modules.service.ILogService;
import com.ssm.modules.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/log")
public class LogController {

    @Autowired
    private ILogService logService;
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/getAllLog", method = RequestMethod.GET)
    public String getAllLog(@RequestParam(value = "page", defaultValue = "-1") int page,
                            @RequestParam(value = "pageSize", defaultValue = "15") int pageSize,
                            Map<String, Object> map) {
        try {
            List<Log> logList = logService.findAll(page, pageSize);

            int amount = logService.findAllCount();

            map.put("page", page);
            map.put("logList", logList);
            map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));

            return "/admin/userVisit";
        }catch (Exception e){
            return "/error/404";
        }
    }

    @RequestMapping(value = "/getLog", method = RequestMethod.GET)
    public String getLog(@RequestParam("userId") int userId,
                         @RequestParam(value = "page", defaultValue = "-1") int page,
                         @RequestParam(value = "pageSize", defaultValue = "15") int pageSize,
                         Map<String, Object> map) {
        try {
            List<Log> logList = logService.findByUserId(userId, page, pageSize);

            int amount = logService.findByUserIdCount(userId);

            map.put("page", page);
            map.put("logList", logList);
            map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));

            return "/personalVisit";
        }catch (Exception e){
            return "/error/404";
        }
    }

    @RequestMapping(value = "/getUserStatistics", method = RequestMethod.GET)
    public String getUserStatistics(@RequestParam(value = "page" ,defaultValue = "-1")int page,
                                    @RequestParam(value = "pageSize" ,defaultValue = "15")int pageSize,
                                    Map<String, Object> map) {
        List<User> allUser = userService.findAllUser(page, pageSize);
        //  获取总数
        int amount = userService.findAllUserCount();
        List<Integer> usersVisitCount = logService.getUsersVisitCount(allUser);

        for(int i = 0; i < allUser.size(); i++) {
            try{
                allUser.get(i).setVisitCount(usersVisitCount.get(i));
            }catch (Exception e){
                allUser.get(i).setVisitCount(0);
            }
        }

        map.put("page", page);
        map.put("allUser", allUser);
        map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));
        return "accessStatistics";
    }

}
